package org.aersoft.jokebook.dao;

import java.util.ArrayList;

import org.aersoft.jokebook.beans.ItemBean;
import org.aersoft.jokebook.dao.api.IAppDAO;
import org.aersoft.jokebook.database.DatabaseConstants;
import org.aersoft.jokebook.database.DatabaseHelper;
import org.aersoft.jokebook.database.DatabaseQueries;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * Class DAO to the recover information of database and support the application
 * needs.
 * @author Danilo de Godoy Carolino.
 */
public class AppDAO implements IAppDAO {

	/**
	 * Persistence helper of {@link DatabaseHelper} type.
	 */
	private DatabaseHelper helper;

	/**
	 * A instance to execute the SQL queries on the database of the
	 * {@link SQLiteDatabase} type.
	 */
	private SQLiteDatabase sql;

	@Override
	public ArrayList<ItemBean> getJokeListByCategory(int id) {
		try {
			Cursor cursor = sql.rawQuery(DatabaseQueries.queryFirstMethodDAO + id, null);
			ArrayList<ItemBean> jokeList = new ArrayList<ItemBean>();
			while (cursor.moveToNext()) {
				ItemBean bean = new ItemBean();
				Cursor cursorCategory = sql.rawQuery(DatabaseQueries.queryCategoryById + cursor.getInt(0), null);
				while (cursorCategory.moveToNext()) {
					bean.setCategoryId(cursorCategory.getInt(0));
					bean.setCategoryName(cursorCategory.getString(1));
					bean.setCategoryRole(cursorCategory.getString(2));
				}
				bean.setContent(cursor.getString(1));
				bean.setId(cursor.getInt(2));
				bean.setTitle(cursor.getString(3));
				bean.setDescription(cursor.getString(4));
				bean.setAuthor(cursor.getString(5));
				bean.setDate(cursor.getString(6));
				jokeList.add(bean);
			}
			cursor.close();
			return jokeList;
		} catch (Exception e) {
			Log.d(DatabaseConstants.AppDAO, e.getMessage());
		}
		return null;
	}

	@Override
	public ItemBean getJokeById(int id) {
		Cursor cursor = sql.rawQuery(DatabaseQueries.querySecondMethodDAO + id, null);
		ItemBean bean = new ItemBean();
		while (cursor.moveToNext()) {
			bean.setCategoryId(cursor.getInt(0));
			bean.setContent(cursor.getString(1));
			bean.setId(cursor.getInt(2));
			bean.setTitle(cursor.getString(3));
			bean.setDescription(cursor.getString(4));
			bean.setAuthor(cursor.getString(5));
			bean.setDate(cursor.getString(6));
		}
		cursor.close();
		return bean;
	}

	@Override
	public ArrayList<ItemBean> getJokeAllCategories() {
		Cursor cursor = sql.rawQuery(DatabaseQueries.getAllCategories, null);
		ArrayList<ItemBean> beanArray = new ArrayList<ItemBean>();
		while (cursor.moveToNext()) {
			ItemBean bean = new ItemBean();
			bean.setCategoryId(cursor.getInt(0));
			bean.setCategoryName(cursor.getString(1));
			beanArray.add(bean);
		}
		cursor.close();
		return beanArray;
	}

	@Override
	public void close() {
		sql.close();
	}

	@Override
	public AppDAO getContext(Context context) {
		this.helper = new DatabaseHelper(context);
		this.sql = helper.getReadableDatabase();
		return this;
	}

}
